package com.example.batch.listen;

import com.example.batch.dao.PersonRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.listener.JobExecutionListenerSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Order(2)
@Slf4j
@Component(value = "jobListen")
public class JobCompletionNotificationListener extends JobExecutionListenerSupport {
    @Autowired
    private PersonRepository repository;

    @Override
    public void beforeJob(JobExecution jobExecution) {
        super.beforeJob(jobExecution);
    }

    @Override
    public void afterJob(JobExecution jobExecution) {
        if (jobExecution.getStatus() == BatchStatus.COMPLETED) {
            log.info("!!! JOB FINISHED! Time to verify the results");
            repository.findAll().parallelStream().forEach(person -> log.info("Found <" + person + "> in the database."));
        }
    }
}
